package demo2;

public enum TestEnum {
    // 枚举对象
    Red("红色",1),
    Yellow("黄色",3),
    blue("蓝色",2);
    private String name;

    private int ordinal;

    TestEnum(String name, int ordinal) {
        this.name = name;
        this.ordinal = ordinal;
    }

    public static void main(String[] args) {
        // values() 方法访问枚举对象，存入数组
        TestEnum[] testEnum= TestEnum.values();
        for (int i = 0; i < testEnum.length; i++) {
            // ordinal() 获取枚举成员的索引位置: 即存储顺序
            System.out.println(testEnum[i].ordinal());
        }

        // valueOf() 将普通字符串转换为枚举实例
        System.out.println(valueOf("blue"));

        // compareTo() : 比较两枚举实例的存储顺序
        System.out.println(blue.compareTo(Red));
        System.out.println(blue.compareTo(Yellow));
        System.out.println(Red.compareTo(blue));
    }
}
